package com.mychat;

import com.mychat.redis.RedisUtils;
import com.mychat.websocket.netty.NettyWebSocketStarter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

@Component("initRun")
public class InitRun implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(InitRun.class);

    @Resource
    private DataSource dataSource;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private NettyWebSocketStarter nettyWebSocketStarter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            dataSource.getConnection();
            redisUtils.get("test");
            new Thread(nettyWebSocketStarter).start();
            logger.info("服务器启动成功，开始愉快的开发吧！");
        } catch(SQLException e) {
            logger.error("数据库连接失败，请检查数据库配置！");
        } catch(RedisConnectionFailureException e) {
            logger.error("Redis连接失败，请检查Redis配置！");
        } catch(Exception e) {
            logger.error("服务器启动失败，错误信息:",e);
        }

    }

}
